package com.game.modular.game.playerMatch.service.base;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.game.modular.game.playerInfo.domain.PlayerMatch;
import com.game.modular.game.playerInfo.mapper.PlayerMatchMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * 基础服务-玩家对局信息
 *
 * @author cxy
 * @since 2024-09-12 19:49:54
 */
@Slf4j
@Service
public class PlayerMatchBaseServiceImpl extends ServiceImpl<PlayerMatchMapper, PlayerMatch> {
    private static final String cacheKey = "player:%d:match";
    @Resource
    private RedisTemplate<Object, Object> redisTemplate;
    @Resource
    private ValueOperations<Object, PlayerMatch> valueOperations;
    @Resource
    private PlayerMatchMapper playerMatchMapper;


    ///////////////////////////////////////////////////////////////////////////////////////////////////////////  基础查询
    public PlayerMatch get(Long playerId) {
        String key = String.format(cacheKey, playerId);
        PlayerMatch playerMatch = valueOperations.get(key);
        if (playerMatch == null) {
            playerMatch = playerMatchMapper.selectById(playerId);
            if (playerMatch == null) {
                return null;
            }
            valueOperations.set(key, playerMatch, 1, TimeUnit.DAYS);
            redisTemplate.expire(key, 60, TimeUnit.MINUTES);
        }
        return playerMatch;
    }

    public void flushList(PlayerMatch Condition) {
        LambdaQueryWrapper<PlayerMatch> wrapper = new LambdaQueryWrapper<>(Condition);
        List<PlayerMatch> playerMatches = playerMatchMapper.selectList(wrapper);
        for (PlayerMatch playerMatch : playerMatches) {
            savaCache(playerMatch);
        }
    }
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////    转换
    public Map<String, PlayerMatch> listToMap(List<PlayerMatch> list) {
        return list.stream().collect(Collectors.toMap(
                k -> String.format(cacheKey, k.getUid()),
                v -> v
        ));
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////  更新插入
    @Override
    public boolean updateById(PlayerMatch entity) {
        boolean b = super.updateById(entity);
        PlayerMatch byId = super.getById(entity.getUid());
        savaCache(byId);
        return b;
    }

    @Override
    public boolean save(PlayerMatch entity) {
        boolean save = super.save(entity);
        entity = super.getById(entity.getUid());
        savaCache(entity);
        return save;
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////  缓存部分
    public void savaCache(Map<String, PlayerMatch> map) {
        map.forEach((k, v)-> valueOperations.set(k, v, 60, TimeUnit.MINUTES));
    }
    public void savaCache(PlayerMatch entity) {
        String key1 = String.format(cacheKey, entity.getUid());
        valueOperations.set(key1, entity, 60, TimeUnit.MINUTES);
    }

    public PlayerMatch selectCache(
            @NotNull(message = "playerId不能为空") Long playerId
    ) {
        String key1 = String.format(cacheKey, playerId);
        redisTemplate.expire(key1, 60, TimeUnit.MINUTES);
        return valueOperations.get(key1);
    }

    public void removeCache(PlayerMatch entity) {
        String key1 = String.format(cacheKey, entity.getUid());
        redisTemplate.delete(key1);
    }
}
